package com.zry.demo;

import java.util.Arrays;

public class QuickSort {
    public static void quickSort(int[] arr, int start, int end) {
        if (arr == null || arr.length <= 1) {
            return;
        }

        if (start >= end) {
            return;
        }

        int temp = arr[start];
        int i = start, j = end;
        while (i != j) {
            while (i < j && arr[j] >= temp) {
                j--;
            }
            while (i < j && arr[i] <= temp) {
                i++;
            }

            int swap = arr[i];
            arr[i] = arr[j];
            arr[j] = swap;
        }
        arr[start] = arr[i];
        arr[i] = temp;
        quickSort(arr, start, i - 1);
        quickSort(arr, i + 1, end);
    }

    public static void main(String[] args) {
        int[] arr = {-4, 2, 10, 9, 8, 7, 6};
        quickSort(arr, 0, arr.length - 1);
        System.out.println(Arrays.toString(arr));

        int result = arr[2];
        System.out.println("第三小的数为" + result);
    }
}
